package com.example.driptip.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.widget.SwitchCompat;
import com.example.driptip.R;
import com.example.driptip.utils.PermissionUtils;

public class HomeSettingsDialog extends Dialog {
    private static final int REQUEST_OVERLAY_PERMISSION = 1001;
    
    private SwitchCompat switchPopup;
    private ImageView ivWarning;
    private SwitchCompat switchSmsSound;
    private SwitchCompat switchVibration;
    private TextView tvPermissionExplanation;
    
    public HomeSettingsDialog(Context context) {
        super(context);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_home_settings);
        
        initViews();
        setupListeners();
        updatePermissionStatus();
    }
    
    private void initViews() {
        switchPopup = findViewById(R.id.switch_popup);
        ivWarning = findViewById(R.id.iv_warning);
        switchSmsSound = findViewById(R.id.switch_sms_sound);
        switchVibration = findViewById(R.id.switch_vibration);
        tvPermissionExplanation = findViewById(R.id.tv_permission_explanation);
    }
    
    private void setupListeners() {
        switchPopup.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked && !PermissionUtils.hasOverlayPermission(getContext())) {
                requestOverlayPermission();
                switchPopup.setChecked(false);
            }
        });
        
        ivWarning.setOnClickListener(v -> {
            requestOverlayPermission();
        });
    }
    
    private void requestOverlayPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getContext().getPackageName()));
            ((android.app.Activity) getContext()).startActivityForResult(intent, REQUEST_OVERLAY_PERMISSION);
        }
    }
    
    private void updatePermissionStatus() {
        boolean hasPermission = PermissionUtils.hasOverlayPermission(getContext());
        switchPopup.setEnabled(hasPermission);
        ivWarning.setVisibility(hasPermission ? View.GONE : View.VISIBLE);
        tvPermissionExplanation.setVisibility(hasPermission ? View.GONE : View.VISIBLE);
        
        // 如果没有权限，确保开关是关闭的
        if (!hasPermission) {
            switchPopup.setChecked(false);
            // 保存设置
            getContext().getSharedPreferences("home_settings", Context.MODE_PRIVATE)
                .edit()
                .putBoolean("popup_enabled", false)
                .apply();
        }
    }
    
    public void onPermissionResult() {
        updatePermissionStatus();
    }
} 